<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class RentModel extends AbstractModel
{
    protected $tableName = 't_rent';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getRent($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addRent($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('动态表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colVarChar('title', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('房屋名称');
        $table->colInt('rent_type', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('出租类型');
        $table->colInt('amount_type', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('付款类型');
        $table->colInt('house_type', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('户型');
        $table->colInt('floor_type', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('楼层');
        $table->colInt('arrow_type', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('方向类型');
        $table->colInt('renovation_type', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('装修类型');
        $table->colInt('is_anxuan', 8)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否安选：0不是，1是');
        $table->colVarChar('area', 10)->setDefaultValue('')->setIsNotNull()->setColumnComment('面积');
        $table->colVarChar('equipment_type', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('设备');
        $table->colVarChar('span', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('标签');
        $table->colVarChar('content', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('房屋介绍');
        $table->colVarChar('location', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('坐标');
        $table->colVarChar('address', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('所在位置');
        $table->colVarChar('price', 255)->setDefaultValue('')->setIsNotNull()->setColumnComment('租金');
        $table->colText('images')->setColumnComment('图片');
        $table->colInt('sid', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('客服id');
        $table->colInt('see', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('浏览');
        $table->colInt('like', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('点赞');
        $table->colInt('comment', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('评论');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('cuid_index', 'uid');
        $table->indexNormal('type_index', 'rent_type');
        $table->indexNormal('is_anxuan_index', 'is_anxuan');
        $table->indexNormal('renovation_type_index', 'renovation_type');
        $table->indexNormal('arrow_type_index', 'arrow_type');
        $table->indexNormal('house_type_index', 'house_type');
        $table->indexNormal('see_index', 'see');
        $table->indexNormal('like_index', 'like');
        $table->indexNormal('comment_index', 'comment');

        return $table;
    }
}